package com.yanqu.road.server.manger.union.listener;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.task.args.union.UnionRechargeUserArgs;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.UnionActivityGroupModule;
import com.yanqu.road.server.gameplayer.module.union.cross.CrossUnionModule;
import com.yanqu.road.server.manger.ICrossUnionSyncListener;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.manger.union.UnionMgr;

import java.util.List;
import java.util.Map;

/**
 * 成员成就监听
 */
public class UnionMemberEventListener implements ICrossUnionSyncListener {

    @Override
    public void notifySync(int syncType, long playerId, CrossUnionInfo unionInfo, List<CrossUnionMember> memberList) {

        int sameMemberNameCount = CrossUnionMgr.getSameNicknameCount(unionInfo);
        if(syncType != eUnionSyncType.DeleteUnion.getValue()){
            Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(unionInfo.getUnionUid());
            if(memberMap != null){
                for (CrossUnionMember member : memberMap.values()) {
                    if(member.getServerId() != GameServer.getInstance().getServerId()){
                        continue;
                    }
                    GamePlayer player = GamePlayerMgr.getOnlinePlayer(member.getUserId());
                    if (null != player) {
                        unionMemberAchievement(unionInfo, sameMemberNameCount, player);
                    }
                }
            }
        }

        if(syncType == eUnionSyncType.QuitUnion.getValue() || syncType == eUnionSyncType.BeKick.getValue()){
            for (CrossUnionMember member : memberList) {
                if (member.getServerId() != GameServer.getInstance().getServerId()) {
                    continue;
                }
                GamePlayer player = GamePlayerMgr.getOnlinePlayer(member.getUserId());
                if(player != null){
                    player.getModule(UnionActivityGroupModule.class).syncAble();
                }
            }
        }

        if(syncType == eUnionSyncType.JoinUnion.getValue()){
            for (CrossUnionMember member : memberList) {
                if(member.getServerId() != GameServer.getInstance().getServerId()){
                    continue;
                }
                if(member.getQuitTime() <= 0){
                    UserInfo userInfo = UserMgr.getUserInfo(member.getUserId());
                    if(userInfo != null) {
                        CrossUnionMgr.sendFirstJoinReward(userInfo);
                    }
                }

                //下发议事厅
                GamePlayer player = GamePlayerMgr.getOnlinePlayer(member.getUserId());
                if(player != null){
                    player.getModule(CrossUnionModule.class).syncChamberPatronsList();
                    UnionMgr.syncCrossTradeWarInfo(unionInfo.getUnionUid(), member.getUserId());
                    UnionRechargeUserArgs args = new UnionRechargeUserArgs(UnionRechargeUserArgs.VALUE_IN, 0, unionInfo.getUnionUid(), member.getUserId());
                    player.notifyListener(eGamePlayerEventType.UnionRechargeUser.getValue(), args);
                }
            }
        }
    }

    private void unionMemberAchievement(CrossUnionInfo unionInfo, int sameMemberNameCount, GamePlayer player) {
        player.notifyListener(eGamePlayerEventType.UnionMemberCount.getValue(), unionInfo.getMemberCount());
        player.notifyListener(eGamePlayerEventType.UnionSameNickname.getValue(), sameMemberNameCount);
    }
}
